package com.learn.learnzookeeper;

import org.apache.zookeeper.*;
import org.apache.zookeeper.client.ZKClientConfig;
import org.apache.zookeeper.data.ACL;

import java.io.IOException;
import java.util.List;

/**
 * @author wangxing
 * @version 2020/7/8 9:33 Administrator
 */
public class ZookeeperLearn {
    private static volatile ZooKeeper zooKeeper = null;
    private static String CONNECT_STRING = "192.168.1.150:2181";

    //    private static Object wait=new Object();
    public static ZooKeeper getZookeeper() throws IOException {
        if (zooKeeper == null) {
            synchronized (CONNECT_STRING) {
                if (zooKeeper == null) {
                    int sessionTimeout = 6000;
                    Watcher watcher = new Watcher() {
                        @Override
                        public void process(WatchedEvent event) {
//                            synchronized (wait){
                            System.out.println(event);
                            System.out.println(event.getPath());
                            try {
                                zooKeeper.getData(event.getPath(), this, null);//进行自监听
                            } catch (KeeperException e) {
                                e.printStackTrace();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
//                                wait.notify();
//                            }
                        }
                    };
                    zooKeeper = new ZooKeeper(CONNECT_STRING, sessionTimeout, watcher);
                }
            }
        }
        return zooKeeper;
    }

    public static void getData(String path, boolean watch) throws KeeperException, InterruptedException, IOException {
        System.out.println("获取数据star");
        byte[] data = getZookeeper().getData(path, watch, null);
        System.out.println("---------------->" + new String(data));
        System.out.println("获取数据end ");
    }

    /**
     * 监听数据变动
     *
     * @param path
     * @throws KeeperException
     * @throws InterruptedException
     * @throws IOException
     */
    public static void getDataWatch(String path) throws KeeperException, InterruptedException, IOException {
        getData(path, true);
    }

    public static void getData(String path) throws KeeperException, InterruptedException, IOException {
        getData(path, false);
    }

    public static void create(String path, byte[] data, List<ACL> acl, CreateMode createMod) throws IOException,
            KeeperException, InterruptedException {
        getZookeeper().create(path, data, acl, createMod);
    }

    /**
     * 获取子节点
     *
     * @param path
     * @param watch
     * @throws KeeperException
     * @throws InterruptedException
     */
    public static void getChildren(String path, boolean watch) throws KeeperException, InterruptedException,
            IOException {
        List<String> childrenList = null;
        if (watch) {
            Watcher watcher = event -> {
                System.out.println(event.getPath());
            };
            childrenList = getZookeeper().getChildren(path, watcher);
        } else {
            childrenList = getZookeeper().getChildren(path, false);
        }
        childrenList.stream().forEach(System.out::println);
    }


}
